package com.lnj.service;

import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingSearchRequest;
import dev.langchain4j.store.embedding.EmbeddingStore;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service
public class ProductSearchService {

    private final EmbeddingModel embeddingModel;
    private final EmbeddingStore<TextSegment> embeddingStore;
    private final int MAX_RESULTS = 5;

    public ProductSearchService(EmbeddingModel embeddingModel,
                                EmbeddingStore<TextSegment> embeddingStore) {
        this.embeddingModel = embeddingModel;
        this.embeddingStore = embeddingStore;
    }

    public List<String> searchProductIdsByKeyword(String keyword) {
        // 1. 将关键词向量化
        Embedding queryEmbedding = embeddingModel.embed(keyword).content();

        // 2. 构建搜索请求
        EmbeddingSearchRequest request = EmbeddingSearchRequest.builder()
                .queryEmbedding(queryEmbedding)
                .maxResults(MAX_RESULTS)
                .build();

        // 3. 执行搜索并提取商品ID
        return embeddingStore.search(request).matches().stream()
                .map(match -> extractProductId(match.embedded().text()))
                .collect(Collectors.toList());
    }

    private String extractProductId(String productText) {
       return productText;
    }
}
